package com.doeis.uwork.dao;

import java.util.ArrayList;
import java.util.List;

import com.google.inject.Singleton;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

@Singleton
public class MessageDao extends MongoDao {

	public <T> List<DBObject> querySlice(String dbname, Class<T> beanClass,Integer start,Integer limit , DBObject query,
			DBObject sort) throws Exception {
		DBCollection coll = getCollection(dbname, beanClass);
		DBCursor cur;
		if (query != null) {
			cur = coll.find(query);
		} else {
			cur = coll.find();
		}
		
		if (sort != null) {
			cur.sort(sort);
		}
		
		if(start != null){
			if (start.intValue() == 0) {
				cur.limit(limit);
			} else {
				if(limit != null){
					cur.skip(start).limit(limit);
				}else{
					cur.skip(start);
				}
			}
		}

		List<DBObject> list = null;
		if (cur != null) {
			list = (List<DBObject>) cur.toArray();
		}else{
			list = new ArrayList<DBObject>();
		}
		return list;
	}
}
